package com.itheima.controller;


import cn.itheima.health.entity.Result;
import cn.itheima.health.resources.MessageConstant;
import cn.itheima.health.resources.RedisMessageConstant;
import cn.itheima.health.resources.SMSUtils;
import cn.itheima.health.resources.ValidateCodeUtils;
import cn.itheima.health.service.OrderService;
import cn.itheima.health.service.SetmealService;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

@Reference
private OrderService orderService;

@Autowired
private JedisPool jedisPool;



    @RequestMapping("/sendCodeToPhone.do")
    public Result sendCodeToPhone(@RequestParam("telephone") String  telephone){
        try {
            //  用户手机发送随机验证码
            String  code  = ValidateCodeUtils.generateValidateCode(4);
            //  保存redis  90s
            Jedis jedis = jedisPool.getResource();
            jedis.setex(telephone+"_"+ RedisMessageConstant.SENDTYPE_ORDER,90,code);
            jedis.close();
            //  发送短信
//            SMSUtils.sendCode(telephone,code);
            System.out.println("---send code ==="+code);
            return  new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }


    @RequestMapping("/validateCodeByTelephone.do")
    public Result validateCodeByTelephone(@RequestParam("telephone") String  telephone,@RequestParam("code") String code){
        try {

            Jedis jedis = jedisPool.getResource();

            String redisCode = jedis.get(telephone + "_" + RedisMessageConstant.SENDTYPE_ORDER);
            if(redisCode==null||redisCode.length()==0){
                return  new Result(false, "验证码已经过期");
            }
            if(!code.equals(redisCode)){
                //  4位  但是 用户输入验证码和redis 不一致
                return  new Result(false, "验证码错误");
            }

            jedis.close();
            //  发送短信
            return  new Result(true,"验证码正确");
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

    }




 @RequestMapping("/add.do")
    public Result add(@RequestBody Map map ){
        try {
            // map   套餐id   + 订单  +  用户信息
           Order order =  orderService.add(map);
            return  new Result(true,MessageConstant.ADD_ORDER_SUCCESS,order);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return  new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.ADD_ORDER_FAIL);
        }

    }

    @RequestMapping("/findOrderInfoByOrderId.do")
    public Result findOrderInfoByOrderId(@RequestParam("id") Integer id ){
        try {
            Map map  = orderService.findOrderInfoByOrderId(id);
            return  new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }

    }


}
